package com.toocms.jinjuhang.ui.advert_list

import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.utils.CommonUtil
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.data.center.VideoData
import com.toocms.jinjuhang.databinding.FgtAdvertDetailsBinding
import com.toocms.jinjuhang.ui.base.BaseFgt
import com.toocms.tab.TooCMSApplication
import com.toocms.tab.imageload.ImageLoader


class AdvertDetailsFgt : BaseFgt<FgtAdvertDetailsBinding, AdvertDetailsModel>() {
    override fun onFragmentCreated() {
        topBar.visibility = View.GONE
        initVideo()
    }

    override fun getLayoutResId() = R.layout.fgt_advert_details

    override fun getVariableId() = BR.advertDetailsModel

    override fun viewObserver() {
        viewModel.loadVideoSingleLiveEvent.observe(this) {
            bindVideoInfo(it)
        }
    }

    override fun getViewModel(): AdvertDetailsModel {
        return AdvertDetailsModel(TooCMSApplication.getInstance(), arguments)
    }

    override fun onBackPressed() {
//        if (!viewModel.isViewEnabledObservableBoolean.get()) return
        if (GSYVideoManager.backFromWindowFull(context)) {
            return
        }
        super.onBackPressed()
    }

    override fun onPause() {
        super.onPause()
        GSYVideoManager.onPause()
    }

    override fun onResume() {
        super.onResume()
        GSYVideoManager.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        GSYVideoManager.releaseAllVideos()
    }

    /**
     * 初始话视频控件
     */
    private fun initVideo() {
        binding.videoSvp.apply {
            showBottomContainer(false)
            setIsTouchWiget(false)
            setThumbPlay(true)
            isAutoFullWithSize = true
            isStartAfterPrepared = true
            titleTextView.visibility = View.GONE
            backButton.visibility = View.GONE
            fullscreenButton.visibility = View.GONE
            if (null == thumbImageView) {
                thumbImageView = ImageView(context).apply {
                    layoutParams = RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT,
                        RelativeLayout.LayoutParams.MATCH_PARENT
                    )
                    scaleType = ImageView.ScaleType.CENTER_INSIDE
                }
            }
            onProgressListener = { progress, count ->
                if (viewModel.isCompleteObservableBoolean.get()) {
                    viewModel.isCompleteObservableBoolean.set(progress == count)
                    viewModel.setView()
                }
                if (progress < viewModel.termLong) {
                    viewModel.isViewEnabledObservableBoolean.set(false)
                    viewModel.timeRemainingObservableField.set(SpannableStringBuilder("${(viewModel.termLong - progress) / 1000L}s").apply {
                        setSpan(
                            ForegroundColorSpan(0xFFFF3333.toInt()),
                            0,
                            length,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                        )
                        append("后可以获取奖励")
                    })
                } else {
                    viewModel.isViewEnabledObservableBoolean.set(true)
                    if (!viewModel.isGetPoint) {
                        viewModel.isGetPoint = true
                        viewModel.videoPoint()
                    }
                }
                viewModel.currentTimeObservableField.set(
                    "${CommonUtil.stringForTime(progress)}/${
                        CommonUtil.stringForTime(
                            count
                        )
                    }"
                )
            }
        }
    }

    private fun bindVideoInfo(data: VideoData) {
        binding.videoSvp.apply {
            setUp(data.file_url, true, null, null, null)
            if (thumbImageView is ImageView) {
                ImageLoader.loadUrl2Image(data.file_poster, thumbImageView as ImageView, 0)
            }
            startPlayLogic()
        }
    }


}